package com.feng.vhr.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.feng.vhr.pojo.Menu;
import com.feng.vhr.response.Rmenu;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author feng
 * @since 2022-05-09
 */
public interface MenuMapper extends BaseMapper<Menu> {

    @Select("select max(rid)rid,id,max(url)url,max(path)path,max(component)component,max(name)name,max(iconCls)iconCls,max(keepAlive)keepAlive,max(requireAuth)requireAuth,max(enabled)enabled from (SELECT distinct hrr.rid,m1.* FROM menu m1 ,menu m2,hr_role hrr,menu_role mr  where m1.id = m2.parentId and hrr.hrid=#{uid} and hrr.rid=mr.rid and mr.mid=m2.id and m2.enabled=true  ) as mm GROUP BY id ORDER BY id")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "url", property = "url"),
            @Result(column = "path", property = "path"),
            @Result(column = "component", property = "component"),
            @Result(column = "name", property = "name"),
            @Result(column = "iconCls", property = "iconCls"),
            @Result(column = "keepAlive", property = "meta.keepAlive"),
            @Result(column = "requireAuth", property = "meta.requireAuth"),
//            @Result(column = "parentId", property = "parentId"),
            @Result(column = "enabled", property = "enabled"),

            @Result(
                    column = "{rid=rid,id=id}",
                    property = "children",
                    javaType = List.class,
                    one = @One(select = "com.feng.vhr.mapper.MenuMapper.getMenuChildrenByRole")
            )
    })
    public List<Rmenu> getMenuByRole(int uid);


    @Select("select m.id,url,path,component,m.name,iconCls,keepAlive,requireAuth,parentId,enabled from menu m,menu_role mr where mr.rid=#{rid} and mr.mid=m.id and m.parentId=#{id} ORDER BY id")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "url", property = "url"),
            @Result(column = "path", property = "path"),
            @Result(column = "component", property = "component"),
            @Result(column = "name", property = "name"),
            @Result(column = "iconCls", property = "iconCls"),
            @Result(column = "keepAlive", property = "meta.keepAlive"),
            @Result(column = "requireAuth", property = "meta.requireAuth"),
            @Result(column = "parentId", property = "parentId"),
            @Result(column = "enabled", property = "enabled"),
            /*@Result(
                    column = "{rid,id}",
                    property = "children",
                    javaType = List.class,
            one = @One(select = "com.feng.vhr.mapper.MenuMapper.getMenuChildrenByRole")
            )*/
    })
    public List<Rmenu> getMenuChildrenByRole(@Param("rid") int rid,@Param("id") int id);



    @Select("select distinct m.id,url,path,component,m.name,iconCls,keepAlive,requireAuth,parentId,enabled from menu m  where m.parentId=1 ")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "url", property = "url"),
            @Result(column = "path", property = "path"),
            @Result(column = "component", property = "component"),
            @Result(column = "name", property = "name"),
            @Result(column = "iconCls", property = "iconCls"),
            @Result(column = "keepAlive", property = "meta.keepAlive"),
            @Result(column = "requireAuth", property = "meta.requireAuth"),
            @Result(column = "parentId", property = "parentId"),
            @Result(column = "enabled", property = "enabled"),

            @Result(
                    column = "id",
                    property = "children",
                    javaType = List.class,
                    one = @One(select = "com.feng.vhr.mapper.MenuMapper.getMenuChildren")
            )
    })
    public List<Rmenu> getAllMenu();

    @Select("select DISTINCT id,url,path,component,name,iconCls,keepAlive,requireAuth,parentId,enabled from menu where parentId=#{id}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "url", property = "url"),
            @Result(column = "path", property = "path"),
            @Result(column = "component", property = "component"),
            @Result(column = "name", property = "name"),
            @Result(column = "iconCls", property = "iconCls"),
            @Result(column = "keepAlive", property = "meta.keepAlive"),
            @Result(column = "requireAuth", property = "meta.requireAuth"),
            @Result(column = "parentId", property = "parentId"),
            @Result(column = "enabled", property = "enabled"),
            /*@Result(
                    column = "{rid,id}",
                    property = "children",
                    javaType = List.class,
            one = @One(select = "com.feng.vhr.mapper.MenuMapper.getMenuChildrenByRole")
            )*/
    })
    public List<Rmenu> getMenuChildren(int id);

}
